package com.huike.web.controller.clues;

import com.huike.clues.domain.TbActivity;
import com.huike.clues.service.ITbActivityService;
import com.huike.common.core.controller.BaseController;
import com.huike.common.core.domain.AjaxResult;
import com.huike.common.core.page.TableDataInfo;
import com.huike.common.utils.DateUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

import static com.github.pagehelper.page.PageMethod.startPage;

/**
 * Created with IntelliJ IDEA.
 * @Author: Copyright by jiyuzm
 * @Date: 2023/07/12/11:58
 * @Description: 活动相关接口-TbActivityController
 */

@RestController
@AllArgsConstructor
@Api(tags = "活动相关接口")
@RequestMapping("/clues/activity")
public class TbActivityController extends BaseController {

    //构造器自动注入
    private final ITbActivityService activityService;

    @GetMapping("/{id}")
    @PreAuthorize("@ss.hasAnyPermi('clues:activity:query')")
    @ApiOperation("获取活动管理详细信息")
    public AjaxResult<TbActivity> getById(@PathVariable Long id){
        TbActivity activity = activityService.getById(id);
        return AjaxResult.success(activity);
    }



    @PostMapping
    @PreAuthorize("@ss.hasAnyPermi('clues:activity:add')")
    @ApiOperation("新增活动")
    public AjaxResult newActivity(@RequestBody TbActivity tbActivity){
        activityService.newActivity(tbActivity);
        return AjaxResult.success();
    }


    @PutMapping
    @PreAuthorize("@ss.hasAnyPermi('clues:activity:edit')")
    @ApiOperation("修改活动")
    public AjaxResult modifyActivity(@RequestBody TbActivity tbActivity){
        activityService.modifyActivity(tbActivity);
        return AjaxResult.success();
    }


    @GetMapping("/list")
    @PreAuthorize("@ss.hasAnyPermi('clues:activity:list')")
    @ApiOperation("查询活动管理列表")
    public TableDataInfo getList (TbActivity tbActivity){
        startPage();
        List<TbActivity> list = activityService.selectActivityList(tbActivity);
        Map<String,Object> countParms = activityService.getCountByStatus();
        return getDataTable(list, countParms);
    }

    @GetMapping("/listselect/{channel}")
    @ApiOperation("获取渠道活动列表")
    public AjaxResult list(@PathVariable("channel") String channel) {
        List<TbActivity> tbActivities = null;
        if (channel != null) {
            TbActivity tbActivity = new TbActivity();
            tbActivity.setChannel(channel);
            tbActivity.setStatus("2");
            Date nowDate = DateUtils.getNowDate();
            tbActivity.setBeginTime(nowDate);
            tbActivity.setEndTime(nowDate);
            tbActivities = activityService.selectActivityList(tbActivity);
        }
        return AjaxResult.success(tbActivities);
    }

    @DeleteMapping("/{ids}")
    @ApiOperation("删除活动接口")
    public AjaxResult deleteActivity(@PathVariable Long[] ids){
        int delete = activityService.deleteActivityByIds(ids);
        return AjaxResult.success(delete);
    }
}
